// 课程
import { request } from '@/api/request';
import { getStudyObjectString } from '@/components/StudyObjectSelect/utils';
import { ShelfProducts } from '../interface';
import Shelf from '../Shelf';

/**
 * @desc 未上架的课程列表返回值
 * @address /safe/api/industry/plan/list
 */
interface unShelfCourseRes {
  courseId: string;
  courseType: string;
  learnObj: string;
  classId: string;
  learnStartTime: string;
  learnEndTime: string;
  state: string;
  industryCode: string;
  industryName: string;
  isDel: string;
  ctDate: string;
  upDate: string;
  courseName: string;
  relationNum: number;
  shelvesCopy: number;
  isShelves: number;
  faceImg?: string;
}

/**
 * @desc 上架的学习计划模版接口的返回值
 * @address /safe/api/shelves/planList
 */
interface onShelfCourseRes {
  guid: string;
  isCharge: number;
  name: string;
  learnObj: string;
  relationNum: number;
  creatTime: string;
  updateTime: string;
}

/**
 * @desc 课程上下架管理
 */
class Course implements ShelfProducts {
  static shelf = new Shelf(3);
  name: string;
  guid: string;
  origin: unShelfCourseRes | onShelfCourseRes;
  learnObjText?: string;
  entTypeCodeText?: string;
  isOnShelf: boolean;

  constructor(id: string, name: string, isOnShelf: boolean, origin: unShelfCourseRes | onShelfCourseRes) {
    this.name = name;
    this.guid = id;
    this.origin = origin;
    this.isOnShelf = isOnShelf;
    this.learnObjText = getStudyObjectString(origin.learnObj);
  }

  get cover() {
    if ('faceImg' in this.origin) {
      return this.origin.faceImg;
    }
  }

  get learnObj() {
    return this.origin.learnObj;
  }

  get createTime() {
    if ('creatTime' in this.origin) {
      return this.origin.creatTime;
    } else {
      return this.origin.ctDate;
    }
  }

  get updateTime() {
    if ('upDate' in this.origin) {
      return this.origin.upDate;
    } else {
      return this.origin.updateTime;
    }
  }

  get num() {
    return this.origin.relationNum;
  }

  get learnEndTime() {
    if ('learnEndTime' in this.origin) {
      return this.origin.learnEndTime;
    }
  }

  get learnStartTime() {
    if ('learnStartTime' in this.origin) {
      return this.origin.learnStartTime;
    }
  }

  get isChargeText() {
    if ('isCharge' in this.origin) {
      return this.origin.isCharge ? '是' : '否';
    }
  }

  static async unShelfList(pageNum: number, pageSize: number, params?: { courseName?: string | number }) {
    const { data } = await request<{ total: number; list: unShelfCourseRes[] }>({
      url: 'safe/api/industry/course/list',
      params: {
        pageNum,
        pageSize,
        ...params,
      },
    });

    return {
      list: data.list.map((v) => {
        return new Course(v.courseId, v.courseName, false, v);
      }),
      total: data.total,
    };
  }
  static async onShelfList(pageNum: number, pageSize: number, params: { learnObj?: string | number }) {
    const { data } = await request<{ total: number; list: onShelfCourseRes[] }>({
      url: '/safe/api/shelves/courseList',
      data: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => {
        return new Course(v.guid, v.name, true, v);
      }),
      total: data.total,
    };
  }

  /**
   * @desc 创建或者更新一个学习计划模版
   */
  static async createOrUpdate({ courseId, ...params }: { courseId?: string } & Record<string, unknown>) {
    await request({
      url: `/safe/api/industry/course/${courseId ? 'update' : 'create'}`,
      data: {
        ...params,
        courseId,
      },
    });
    return true;
  }

  static async delete(courseId: string) {
    await request({
      url: '/safe/api/industry/course/delete',
      params: {
        courseId,
      },
    });
  }
}

export default Course;
